{extend name="adminblue/base" /}
{block name="resources"/}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/defau.css">
<script src="ADMIN_JS/art_dialog.source.js"></script>
<script src="ADMIN_JS/iframe_tools.source.js"></script>
<script src="ADMIN_JS/material_managedialog.js"></script>
<style>
.sql-title{
	height:45px;
	line-height:45px;
	font-size:15px;
}
.sql-block{
	display:block;
	width:95%;
	margin: 0px  auto;
    background: #eee;
	padding:1.5em;
	
/* 	-webkit-border-radius: 4px 4px 0 0; */
    border: #aaa solid 1px;
    background: #eee;
	    box-shadow: 1px 1px 2px #fff inset;
}
.width-block{
	width:80% !important;
	margin: 20px  auto;
	height:250px !important;
}
.textarea-group{
	marign:0 auto;
	width:100% !important;
}
.textarea { 
	background-color: #fff; 
	height: 300px; 
	padding-top: 10px; 
	padding-right: 10px; 
	padding-left: 10px; 
	padding-bottom: 10px; 
	overflow: auto; 
	color: #555; 
	font-size: 16px; 
	width:96% !important;
	border:none;
	border-radius: unset;
}
.textarea:focus{
	color:#555; 
}
.textarea-wrap{
	border-right: 1px solid #ddd;
    white-space: nowrap;
}
.sql-bottom-block{
	width:95%;
	overflow:hidden;
	margin: 20px  auto;
	border:1px  solid #eee;
	margin: 0  auto;
	padding:1.5em;
	border: #aaa solid 1px;
/* 	-webkit-border-radius: 0 0 4px 4px; */
	color: #000;
    background: #D3DCE3;
	border-top: 0;
	text-align:right;
}
.alert{
	height:auto;
}

.h1-error h1 {
    border-color: #ff0000;
}
.h1-error h1 {
    border-bottom: 2px solid;
    font-weight: bold;
    text-align: left;
    margin: 0 0 .2em 0;
	font-size: 140%;
	color:black;
}
.h1-error h1 , .h1-error strong{
	color:black;
}
.error {
    border: 1px solid maroon !important;
    color: #000;
    background: pink;
}
.h1-error pre{
	background: none;
	border:none;
}
.h1-error code {
    white-space: normal;
	background: none;
}
.alert-success {
    color: #3c763d;
    background-color: #dff0d8;
    border-color: #d6e9c6;
}
.sql-file{
	 opacity:0.0;
	filter:Alpha(opacity=0); /* IE8 以及更早的浏览器 */ 
	position:absolute;
	display:block;
	width:86px !important;
	height:26px;
	left:0;
	top:0;
}
.b-f-l{
	float:left;
}
.b-f-r{
	float:right;
}
.button-div{
	position:relative;
}

</style>
{/block}
{block name="main"}
<div class="set-style">
	<div class="alert hide" id="prompt"></div>
	<div class="sql-block">
		<textarea id="text" class="textarea"></textarea>
		<br/>
		<button class="btn-common btn" onclick="$('#text').val('');">清除</button>
	</div>
	<div class="sql-bottom-block">
		<div class="button-div b-f-l"><button class="btn-common">将sql导入</button><input class="sql-file" type="file"  name="file_upload_sql"id="sqlfile"value="" onchange="uploadSqlFile();"/></div>
		
		<button class="btn-common b-f-r"  onclick="sqlQuery();">执行</button>
	</div>
</div>
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="ADMIN_JS/auto-line-number.js"></script>
<script>
$("#text").setTextareaCount({
	width: "25px",
	bgColor: "#f7f7f7",
	color: "#555",
	display: "inline-block"
});

var flag = false;
function sqlQuery(){
	$("#prompt").hide();
	var sql = $("#text").val();
	if($.trim(sql) == ""){
		showMessage("表单中缺少值！");
	}
	if(!flag){
		$.ajax({
			type:"post",
			url:"{:__URL('ADMIN_MAIN/dbdatabase/sqlfilequery')}",
			data:{"sql_text":sql},
			success: function (data) {
				$("#prompt").show();
				if(data.status == 1){
					var html = '';
					html += '<a class="close" data-dismiss="alert">×</a>';
					html += '<span>'+data.message+ '</span>';
					$("#prompt").html(html);
					$("#prompt").removeClass("alert-danger");
					$("#prompt").addClass("alert-success");
				}else{
					var html = '';
					html += '<a class="close" data-dismiss="alert">×</a>';
					html += '<div class="h1-error"><h1>错误</h1>';
					html += '<p><strong>SQL 查询：</strong>';
					html += '</p>';
					html += '<p>';
					html += '<code class="sql"></code></p><pre><code class="sql">'+sql;
					html += '</code></pre>';
					html += '<p></p>';
					html += '<p>';
					html += '<strong>MySQL 返回： </strong>';
					html += '</p>';
					html += '<code>';
					html += data.message;
					html += '</code><br>';
					html += '</div>';
					console.log(html);
					$("#prompt").html(html);
					$("#prompt").addClass("alert-danger");
					$("#prompt").removeClass("alert-success");
				}

			}
		});
	}
	
}

/* 上传文件
*/
function uploadSqlFile(){
	var fileid = "sqlfile";
	var dom = document.getElementById(fileid);
	var file =  dom.files[0];//File对象;
	var name = file.name;
	if(name != ''){
		name = name.split("").reverse().join("")
		if(name.indexOf("lqs.") === 0){
			if(file.type == ""){
				$.ajaxFileUpload({
					url : "{:__URL('APP_MAIN/upload/uploadSql')}", //用于文件上传的服务器端请求地址
					secureuri : false, //一般设置为false
					fileElementId : "sqlfile", //文件上传空间的id属性  <input type="file" id="file" name="file" />
					dataType : 'json', //返回值类型 一般设置为json
					async : false,
					contentType : "text/json;charset=utf-8",
					success : function(res){ //服务器成功响应处理函数
						if(res.code > 0){
							$("#text").val(res.sql_str+"\r\n");
							$("#text").keydown();
						}else{
							showMessage('error', res.message);
						}
					}
				});
			}
		}else{
			showMessage('error', "必须是有效的sql文件！");
		}
	}
}
</script>
{/block}